package org.javaboy.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author 江南一点雨
 * @Site www.javaboy.org 2019-06-01 16:15
 */
@Service
public class HelloService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private String prefexToken="token";

    public String hello(String name) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("k1", name);
        Object k1 = ops.get("k1");
        System.out.println(k1);

        return k1.toString();
    }

    public String hello2(String name) {
        ValueOperations ops = stringRedisTemplate.opsForValue();
        ops.set("k2", name);
        Object k2 = ops.get("k2");
        System.out.println(k2);

        return k2.toString();
    }

    public String setGroup() {
        ValueOperations ops = stringRedisTemplate.opsForValue();
//        ValueOperations ops = redisTemplate.opsForValue();
        for (int i=1;i<=10;i++) {
//            ops.set("token:"+i, "token-"+ LocalDateTime.now(),1, TimeUnit.DAYS);
            ops.set(getRedisKey(prefexToken,String.valueOf(i)), "token-"+ LocalDateTime.now(),1, TimeUnit.DAYS);
        }
        Object u1 = ops.get("token:1");
        Object u2 = ops.get("token:2");
        System.out.println(u1);

        return u2.toString();
    }

    public Map<String,List<String>> getList(){
        List<String> list1=new ArrayList<String>();
        list1.add("a1");
        list1.add("a2");
        list1.add("a3");

        List<String> list2=new ArrayList<String>();
        list2.add("b1");
        list2.add("b2");
        list2.add("b3");

        ListOperations<String,List<String>> listOperations=redisTemplate.opsForList();
        String key1="listkey1";
        String key2="listkey2";
        listOperations.leftPush(key1,list1);
        listOperations.rightPush(key2,list2);

        List<String> resultList1=listOperations.leftPop(key1);
//        List<String> resultList2=listOperations.rightPop(key2);
//        System.out.println("resultList1:"+resultList1);
//        System.out.println("resultList2:"+resultList2);

        Map<String,List<String>> map=new HashMap<>();
        map.put(key1,list1);
        map.put(key2,list2);

        return map;
    }

    public String redisTest(){
        ValueOperations ops = stringRedisTemplate.opsForValue();
//        for (int i=1;i<=10;i++) {
//            ops.set(getRedisKey(prefexToken,String.valueOf(i)), "token-"+ LocalDateTime.now(),1, TimeUnit.DAYS);
//        }
        String key1=getRedisKey(prefexToken,String.valueOf(1));
        String key2=getRedisKey(prefexToken,String.valueOf(2));
        ops.set(key1, "token-"+ LocalDateTime.now(),1, TimeUnit.DAYS);
        ops.set(key2, "token-"+ LocalDateTime.now(),1, TimeUnit.DAYS);
        Object u1 = ops.get(key1);
        Object u2 = ops.get(key2);
        boolean isExisted=stringRedisTemplate.hasKey(key1);
        if(isExisted){
            boolean isDeleted=stringRedisTemplate.delete(key1);
        }

        return "ok";
    }

    private String getRedisKey(String prefix,String key){
        return String.format("%s:%s",prefix,key);
    }

}
